package com.tcoiss.datafactory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tcoiss.datafactory.domain.MqSyncRecord;
import com.tcoiss.datafactory.mapper.MqSyncRecordMapper;
import com.tcoiss.datafactory.service.IMqSyncRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 消息同步记录Service业务层处理
 *
 * @author zw
 * @date 2021-05-13
 */
@Service
public class MqSyncRecordServiceImpl extends ServiceImpl<MqSyncRecordMapper, MqSyncRecord> implements IMqSyncRecordService {

    @Override
    public List<MqSyncRecord> queryList(MqSyncRecord mqSyncRecord) {
        LambdaQueryWrapper<MqSyncRecord> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(mqSyncRecord.getTopicCode())){
            lqw.eq(MqSyncRecord::getTopicCode ,mqSyncRecord.getTopicCode());
        }
        if (StringUtils.isNotBlank(mqSyncRecord.getTopicName())){
            lqw.like(MqSyncRecord::getTopicName ,mqSyncRecord.getTopicName());
        }
        if (StringUtils.isNotBlank(mqSyncRecord.getMsgId())){
            lqw.eq(MqSyncRecord::getMsgId ,mqSyncRecord.getMsgId());
        }
        if (StringUtils.isNotBlank(mqSyncRecord.getOperMethod())){
            lqw.eq(MqSyncRecord::getOperMethod ,mqSyncRecord.getOperMethod());
        }
        if (mqSyncRecord.getSyncTime() != null){
            lqw.eq(MqSyncRecord::getSyncTime ,mqSyncRecord.getSyncTime());
        }
        if (StringUtils.isNotBlank(mqSyncRecord.getMsgBody())){
            lqw.eq(MqSyncRecord::getMsgBody ,mqSyncRecord.getMsgBody());
        }
        lqw.orderByDesc(MqSyncRecord::getSyncTime);
        return this.list(lqw);
    }

    @Override
    public boolean selectIsExists(String topicCode, String msgId) {
        LambdaQueryWrapper<MqSyncRecord> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(topicCode)){
            lqw.eq(MqSyncRecord::getTopicCode ,topicCode);
        }
        if (StringUtils.isNotBlank(msgId)){
            lqw.eq(MqSyncRecord::getMsgId ,msgId);
        }
        if(this.list(lqw) != null &&this.list(lqw).size()>0){
            return true;
        }
        return false;
    }
}
